import { ref, toRefs, watch } from "vue";
import { getFactoryTree, depeIdSelectList } from "@/api/system/dept";
export function useFactorySelect(type, value) {
  const options = ref([]);
  if (!type) {
    function getFactoryList() {
      getFactoryTree().then((res) => {
        options.value = res.data;
      });
    }
    getFactoryList();
  } else {
    watch(
      () => type,
      (newValue, oldValue) => {
        depeIdSelectList({
          deptType: type,
          deptId: value,
        }).then((res) => {
          workshopOptions.value = res.data;
          lineOptions.value = [];
          processOptions.value = [];
          queryParams.value.workshopCode = "";
        });
      },
      { deep: true }
    );
  }
  


  // watch(
  //   () => workshop,
  //   (newValue, oldValue) => {
  //     const result = workshopOption.value.filter(
  //       (el) => el.code == workshop.value
  //     );
  //     lineOption.value = result.length ? result[0].children : [];
  //     processOption.value = [];
  //   },
  //   { deep: true }
  // );
  // watch(
  //   () => prodLine,
  //   (newValue, oldValue) => {
  //     const result = lineOption.value.filter((el) => el.code == prodLine.value);
  //     processOption.value = result.length ? result[0].children : [];
  //   },
  //   { deep: true }
  // );

  
  return {
    factoryOptions,
    workshopOption,
    lineOption,
    processOption,
  };
}
